package com.mikgonz.farkle.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.mikgonz.farkle.shared.FarkleRequestFactory;

public class XtifyHelper {
	
	public static void sendFriendRequest(String from, String toKey, String requestId)
			throws IOException {
		talkToXtify(from, toKey, requestId, FarkleRequestFactory.NEW_FRIEND_REQUEST_ACTION);
	}
	
	public static void notifyFriendRequestAccepted (String otherName, String toKey, Long otherId)
	throws IOException {
		talkToXtify(otherName, toKey, Long.toString(otherId), FarkleRequestFactory.FRIEND_REQUEST_ACCEPTED_ACTION);
	}
	
	public static void notifyFriendRequestRejected (String otherName, String toKey)
	throws IOException {
		talkToXtify(otherName, toKey, "", FarkleRequestFactory.FRIEND_REQUEST_REJECTED_ACTION);
	}

	public static void sendChatMesg(String from, String to_key, String body)
			throws IOException {
		talkToXtify(from, to_key, body, FarkleRequestFactory.NEW_MSG_ACTION);
	}

	public static void talkToXtify(String from, String to_key, String body,
			String intent) throws IOException {
		String urlString = "http://notify.xtify.com/api/1.2/pn/push";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<push-notification>" + "<android>"
				+ "<appKey>0fc1b87c-3e37-4f8a-a46c-31e8d5350594</appKey>"
				+ "<secret>27eceeca-6629-4810-862a-4683737fa182</secret>"
				+ "<deviceTokens><token>" + to_key + "</token></deviceTokens>"
				+ "<messageTitle>" + from + "</messageTitle>" + "<message>"
				+ body + "</message>" + "<actionIntent>" + intent
				+ "</actionIntent>" + "<actionType>CUSTOM</actionType>"
				+ "<url>http://www.xtify.com</url>" + "</android>"
				+ "</push-notification>"; 
		String result = null;
		URL url = null;
		try {
			url = new URL(urlString);
		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
		}

		HttpURLConnection urlConn = null;
		OutputStream out = null;
		BufferedReader in = null;
		if (url != null) {
			try {
				urlConn = (HttpURLConnection) url.openConnection();
				urlConn.addRequestProperty("Content-Type", "application/xml");

				urlConn.setRequestMethod("PUT");
				urlConn.setDoOutput(true);
				urlConn.setDoInput(true);
				urlConn.connect();

				// Write content data to server
				out = urlConn.getOutputStream();
				out.write(content.getBytes());
				out.flush();

				// Check response code
				if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {

					in = new BufferedReader(new InputStreamReader(
							urlConn.getInputStream()), 8192);
					StringBuffer strBuff = new StringBuffer();
					String inputLine;

					while ((inputLine = in.readLine()) != null) {
						strBuff.append(inputLine);
					}
					result = strBuff.toString();

					System.err.println(result);

				}

			} catch (IOException e) {
				System.out.println(e.getMessage());
			} finally {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
				if (urlConn != null) {
					urlConn.disconnect();
				}
			}
		}
	}
}
